#include <Excel.au3>
#include <MsgBoxConstants.au3>

#pragma compile(x64, true)
#pragma compile(FileVersion, 1.0.0.0)
#pragma compile(ProductVersion, 1.0.0.0)
#pragma compile(ProductName, "medion_tool.exe")
#pragma compile(LegalCopyright, '(c) Tody, 2019 T-ware Inc.')
#pragma compile(FileDescription, Medion MDA tool)
#pragma compile(Icon, C:\Program Files (x86)\AutoIt3\Icons\MyAutoIt3_Green.ico)

#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>

Global $ORH1="NULL"
Global $ORH2="NULL"
Global $sSF="NULL"
Global $sBBP="NULL"
Global $sBV="NULL"
Global $sSOFTID="NULL"
Global $sOAID="NULL"
Global $sLCD="NULL"
Global $sPN="NULL"
Global $SKU="NULL"

Global $batFile=@ScriptDir&"\WriteDMI.bat"
Global $chkFile=@ScriptDir&"\WriteDMI.bat"
Global $oaFile= @ScriptDir&"\oa3tool.cfg"
Global $ini=@ScriptDir&"\config.ini"
Global $MDFile="NULL"
Global $sMDNo="NULL"
Global $docID="NULL"

Func WriteChkBatch()
    If FileExists($chkFile) Then FileDelete($chkFile)

   FileWriteLine($chkFile, '@echo off')
   FileWriteLine($chkFile, ':: IMAGE_ID: '&$sSOFTID)
   FileWriteLine($chkFile, ':: OA3_PartNumber: '&$sOAID)
   FileWriteLine($chkFile, ':: Part_Number: '&$sPN)
   FileWriteLine($chkFile, ':: Built_Date: '&@YEAR&'-'&@MON&'-'&@MDAY)
   FileWriteLine($chkFile, ':: FROM_File: '&$MDFILE)
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, ':start')
   FileWriteLine($chkFile, '	cd \process\sfc')
   FileWriteLine($chkFile, '	call getsn.bat')
   FileWriteLine($chkFile, '	call getbs.bat')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	cd \testfile\OEMSRV')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, ':chk')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /SP >sp.ini')
   FileWriteLine($chkFile, '	find /i "'&StringStripWS($ORH2, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" sp.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /SK >sk.ini')
   FileWriteLine($chkFile, '	find /i "'&StringStripWS($ORH1, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" sk.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /SS >ss.ini')
   FileWriteLine($chkFile, '	find /i "%snid%" ss.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '	')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /SF >sf.ini')
   FileWriteLine($chkFile, '	find /i "'&StringStripWS($sSF, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" sf.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /BP >bp.ini')
   FileWriteLine($chkFile, '	find /i "'&StringStripWS($sBBP, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" bp.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /BV >bv.ini')
   FileWriteLine($chkFile, '	find /i "'&StringStripWS($sBV, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" bv.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /BS >bs.ini')
   FileWriteLine($chkFile, '	find /i "%bsid%" bs.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /SM >sm.ini')
   FileWriteLine($chkFile, '	find /i "MEDION" sm.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /BM >bm.ini')
   FileWriteLine($chkFile, '	find /i "MEDION" bm.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, '	AMIDEWINx64.exe /CM >cm.ini')
   FileWriteLine($chkFile, '	find /i "MEDION" cm.ini')
   FileWriteLine($chkFile, '	if %errorlevel% == 0 goto pass')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, ':fail')
   FileWriteLine($chkFile, '	color 04')
   FileWriteLine($chkFile, '	ECHO           ----------Check dmi Fail---------------')
   FileWriteLine($chkFile, '	ECHO            XXXXXXX     XXXX    XXXXXXXX  XXX')
   FileWriteLine($chkFile, '	ECHO            XXXXXXX    XXXXXX   XXXXXXXX  XXX')
   FileWriteLine($chkFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($chkFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($chkFile, '	ECHO            XXXXXXX   XXXXXXXX     XX     XXX')
   FileWriteLine($chkFile, '	ECHO            XXXXXXX   XXXXXXXX     XX     XXX')
   FileWriteLine($chkFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($chkFile, '	ECHO            XX        XX    XX  XXXXXXXX  XXXXXXXX')
   FileWriteLine($chkFile, '	ECHO            XX        XX    XX  XXXXXXXX  XXXXXXXX')
   FileWriteLine($chkFile, '	ECHO           -----------------------------------')
   FileWriteLine($chkFile, '	echo            ==  Please press any key to retry  ===')
   FileWriteLine($chkFile, '	pause ')
   FileWriteLine($chkFile, '	color 07')
   FileWriteLine($chkFile, 'goto start')
   FileWriteLine($chkFile, '')
   FileWriteLine($chkFile, ':pass')
   FileWriteLine($chkFile, '	echo  [%date% %time%]: Check dmi pass >>c:\testfile\test.log')
EndFunc

Func WriteBatch()

   If FileExists($batFile) Then FileDelete($batFile)

   FileWriteLine($batFile, '@echo off')
   FileWriteLine($batFile, ':: IMAGE_ID: '&$sSOFTID)
   FileWriteLine($batFile, ':: OA3_PartNumber: '&$sOAID)
   FileWriteLine($batFile, ':: Part_Number: '&$sPN)
   FileWriteLine($batFile, ':: Built_Date: '&@YEAR&'-'&@MON&'-'&@MDAY)
   FileWriteLine($batFile, ':: BOM_File: '&$MDFILE)
   FileWriteLine($batFile, '')
   FileWriteLine($batFile, ':start')
   FileWriteLine($batFile, '	cd \process\sfc')
   FileWriteLine($batFile, '	call getsn.bat')
   FileWriteLine($batFile, '	call getbs.bat')
   FileWriteLine($batFile, '')
   FileWriteLine($batFile, '	cd \testfile\OEMSRV')
   FileWriteLine($batFile, '')
   FileWriteLine($batFile, ':flash')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /SP "'&StringStripWS($ORH2, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /SK "'&StringStripWS($ORH1, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /SF "'&StringStripWS($sSF, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /BP "'&StringStripWS($sBBP, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /BV "'&StringStripWS($sBV, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /BS "%bsid%" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /SM "MEDION" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /BM "MEDION" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 1 goto Fail')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, '	AMIDEWINx64.exe /CM "MEDION" | find /i "Done"')
   FileWriteLine($batFile, '	if %errorlevel% == 0 goto pass')
   FileWriteLine($batFile, '')
   FileWriteLine($batFile, ':fail')
   FileWriteLine($batFile, '	color 04')
   FileWriteLine($batFile, '	ECHO           ----------Writedmi Fail---------------')
   FileWriteLine($batFile, '	ECHO            XXXXXXX     XXXX    XXXXXXXX  XXX')
   FileWriteLine($batFile, '	ECHO            XXXXXXX    XXXXXX   XXXXXXXX  XXX')
   FileWriteLine($batFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($batFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($batFile, '	ECHO            XXXXXXX   XXXXXXXX     XX     XXX')
   FileWriteLine($batFile, '	ECHO            XXXXXXX   XXXXXXXX     XX     XXX')
   FileWriteLine($batFile, '	ECHO            XX        XX    XX     XX     XXX')
   FileWriteLine($batFile, '	ECHO            XX        XX    XX  XXXXXXXX  XXXXXXXX')
   FileWriteLine($batFile, '	ECHO            XX        XX    XX  XXXXXXXX  XXXXXXXX')
   FileWriteLine($batFile, '	ECHO           -----------------------------------')
   FileWriteLine($batFile, '	echo ==    Please press any key to retry      ===')
   FileWriteLine($batFile, '	pause ')
   FileWriteLine($batFile, '	color 07')
   FileWriteLine($batFile, 'goto start')
   FileWriteLine($batFile, '	')
   FileWriteLine($batFile, ':pass')
   FileWriteLine($batFile, '	echo [%date% %time%]: write dmi pass >>c:\testfile\test.log')
EndFunc

Func WriteOACFG()
   If FileExists($oaFile) Then FileDelete($oaFile)

   If StringInStr($sBBP, "GK5") Then
	  $sLCD="15.6"
   Else
	  $sLCD="17.3"
   EndIf

   FileWriteLine($oaFile, '<OA3>')
   FileWriteLine($oaFile, '  <ServerBased>')
   FileWriteLine($oaFile, '    <KeyProviderServerLocation>')
   FileWriteLine($oaFile, '      <IPAddress>172.17.0.58</IPAddress>')
   FileWriteLine($oaFile, '      <ProtocolSequence>ncacn_ip_tcp</ProtocolSequence>')
   FileWriteLine($oaFile, '      <EndPoint>8765</EndPoint>')
   FileWriteLine($oaFile, '      <Options></Options>')
   FileWriteLine($oaFile, '    </KeyProviderServerLocation>')
   FileWriteLine($oaFile, '    <Parameters>')
   FileWriteLine($oaFile, '      <Parameter name="LicensablePartNumber" value="'&StringStripWS($sOAID, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'" />')
   FileWriteLine($oaFile, '         <OEMOptionalInfo>')
   FileWriteLine($oaFile, '              <Field>')
   FileWriteLine($oaFile, '                <Name>ZPC_MODEL_SKU</Name>')
   FileWriteLine($oaFile, '                <Value>'&StringStripWS($ORH1, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&' '&StringStripWS($ORH2, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)&'</Value>')
   FileWriteLine($oaFile, '              </Field>')
   FileWriteLine($oaFile, '              <Field>')
   FileWriteLine($oaFile, '                <Name>ZFRM_FACTOR_CL1</Name>')
   FileWriteLine($oaFile, '                <Value>Notebook</Value>')
   FileWriteLine($oaFile, '              </Field>')
   FileWriteLine($oaFile, '              <Field>')
   FileWriteLine($oaFile, '                <Name>ZFRM_FACTOR_CL2</Name>')
   FileWriteLine($oaFile, '                <Value>Standard</Value>')
   FileWriteLine($oaFile, '              </Field>')
   FileWriteLine($oaFile, '              <Field>')
   FileWriteLine($oaFile, '                <Name>ZSCREEN_SIZE</Name>')
   FileWriteLine($oaFile, '                <Value>'&$sLCD&'</Value>')
   FileWriteLine($oaFile, '              </Field>')
   FileWriteLine($oaFile, '              <Field>')
   FileWriteLine($oaFile, '                <Name>ZTOUCH_SCREEN</Name>')
   FileWriteLine($oaFile, '                <Value>Non-touch</Value>')
   FileWriteLine($oaFile, '              </Field>')
   FileWriteLine($oaFile, '         </OEMOptionalInfo>')
   FileWriteLine($oaFile, '	<BusinessID>77</BusinessID>')
   FileWriteLine($oaFile, '    </Parameters>')
   FileWriteLine($oaFile, '  </ServerBased>')
   FileWriteLine($oaFile, '  <OutputData>')
   FileWriteLine($oaFile, '    <AssembledBinaryFile>OA3.bin</AssembledBinaryFile>')
   FileWriteLine($oaFile, '    <ReportedXMLFile>OA3_OUT.xml</ReportedXMLFile>')
   FileWriteLine($oaFile, '  </OutputData>')
   FileWriteLine($oaFile, '</OA3>')
EndFunc

Func Do_Excel($excFile)
   ; Create application object
   Local $oExcel = _Excel_Open()
   If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel打开错误", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

   ; Open an existing workbook and return its object identifier.
   Local $sWorkbook = $excFile
   Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook, True, False)
   If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel打开错误", "Error opening '" & $sWorkbook & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
   Local $aResult = _Excel_RangeRead($oWorkbook, Default, "C1:D100", 2)
   If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel读取错误", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
   _ArrayDisplay($aResult, "表格内容是否正确？")

   For $i =0 to 99
	  if(StringInStr($aResult[$i][0], "OHR1")) Then
		 $aOHR=StringSplit($aResult[$i][0],":")
		 $ORH1=$aOHR[2]
		 ;MsgBox(0,'ORH1',$ORH1)
	  EndIf

	  if(StringInStr($aResult[$i][0], "OHR2")) Then
		 $aOHR=StringSplit($aResult[$i][0],":")
		 $ORH2=$aOHR[2]
		 ;MsgBox(0,'ORH2',$ORH2)
	  EndIf

	  if(StringInStr($aResult[$i][0], "SF:")) Then
		 $aOHR=StringSplit($aResult[$i][0],":")
		 $sSF=$aOHR[2]
		 ;MsgBox(0,'SF',$sSF)
	  EndIf

	  ;if(StringInStr($aResult[$i][0], "BBP:")) Then
		; $aOHR=StringSplit($aResult[$i][0],":")
		; $sBBP=$aOHR[2]
		 ;MsgBox(0,'BBP',$sBBP)
	  ;EndIf

	  if(StringInStr($aResult[$i][1], "BB TFG")) Then
		 $aOHR=StringSplit(StringStripWS($aResult[$i][1],$STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)," ")
		 $SKU=$aOHR[4]
		 ;MsgBox(0,'SKU',$SKU)
	  EndIf

   if(StringInStr($aResult[$i][1], "SWM")) Then
		 $sSOFTID=$aResult[$i][0]
		 ;MsgBox(0,'SOFTID',$sSOFTID)
	  EndIf

	  if(StringInStr($aResult[$i][1], "MS DPK")) Then
		 $sOAID=$aResult[$i][0]
		 ;MsgBox(0,'OAKEYID',$sOAID)
	  EndIf

   Next

   If Not StringInStr($sPN, $SKU) or Not StringInStr($ORH2, $sMDNo) or Not StringInStr($ORH1, $docID) Then
	  MsgBox($MB_ICONERROR, "Medion MD TOOL", "成品料号与BOM中信息不符或MD、DOC号不匹配！！")
	  DriveMapDel ("X:")
	  Exit
   EndIf

   ;;; read config file for BP and BV
   $sBBP=IniRead($ini, "BP", $SKU, "NULL")
   $sBV =IniRead($ini, "BV", $SKU, "NULL")

   If $sBBP="NULL" 		Then $sBBP=InputBox("主板名", "主板产品名称", "GK5Cx")
   If $ORH1="NULL" 		Then $ORH1=InputBox("OHR1", "OHR1值", "XXXX XXXX")
   If $ORH2="NULL" 		Then $ORH2=InputBox("OHR2", "OHR2值", "XXXX XXXX MDXXXXX")
   If $sSF="NULL" 		Then $sSF=InputBox("System Family", "System Family", "GK5Cx")
   If $sBV="NULL"		Then $sBV=InputBox("主板版本", "主板版本号", "1.3")
   If $sSOFTID="NULL" 	Then $sSOFTID=InputBox("Software ID", "Software ID", "GK5Cx")
   If $sOAID="NULL" 	Then $sOAID=InputBox("OA3 PART", "OA3 PARTNUMBER", "GK5Cx")

   WriteBatch()
   WriteChkBatch()
   WriteOACFG()

   MsgBox($MB_ICONINFORMATION,"成功", "恭喜你，成功创建WriteDMI.bat与OA3TOOL.CFG文件，请确认正确性哦！！")
EndFunc

;;map network Drive
DriveMapDel ("X:")
DriveMapAdd ("X:", "\\10.100.180.41\测试工程课", $DMA_AUTHENTICATION)   ;; mapping bios ec drive
$ret=FileCopy("X:\Medion\Medion_Tool\config.ini", @ScriptDir&"\", $FC_OVERWRITE)  ;; copy files to U disk by PN
if $ret = 1 Then
   MsgBox(64, "成功", "CONFIG.INI更新成功",1)
Else
   MsgBox(16, "失败", "CONFIG.INI更新失败")
EndIf

If Not FileExists($ini) Then
   MsgBox($MB_ICONERROR, "Medion MD TOOL", "CONFIG.INI丢失，程序将退出！！")
   DriveMapDel ("X:")
   Exit
EndIf

$sPN	=InputBox("成品料号", "输入成品料号","FGM-GK7CP0S")
$sMDNo	=InputBox("MD号", "输入MD号码","MD6157x")
$docID	=InputBox("DOC ID号", "输入DOC ID号","3002709x")

If StringLen($sPN)<> 18 or StringLen($sMDNo) <> 7 or StringLen($docID) <> 8 Then
   MsgBox($MB_ICONERROR, "Medion MD TOOL", "成品料号、MD号、DOC号存在错误，程序将退出！！")
   DriveMapDel ("X:")
   Exit
EndIf

$sPN	=StringStripWS($sPN, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)
$sMDNo	=StringStripWS($sMDNo, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)
$docID	=StringStripWS($docID, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES)

DirCreate(@ScriptDir&"\"&$sPN)
$batFile=@ScriptDir&"\"&$sPN&"\WriteDMI.bat"
$chkFile=@ScriptDir&"\"&$sPN&"\ChkDMI.bat"
$oaFile=@ScriptDir&"\"&$sPN&"\oa3tool.cfg"

Local $sFileOpenDialog = FileOpenDialog("打开文件",  @WindowsDir & "\", "Excel (*.xls;*.xlsx)")

Local $aFile = StringSplit($sFileOpenDialog,'\')
$MDFILE=$aFile[$aFile[0]]
;MsgBox(0,'file name',$MDFILE)
local $ret=MsgBox(52, "文件名", "MD文件为："&$sFileOpenDialog&"对吗？")
if ($IDYES = $ret) Then
   Do_Excel($sFileOpenDialog)
   DriveMapDel ("X:")
Else
   MsgBox($MB_ICONINFORMATION,"再见", "欢迎下次再用我哦！！")
   DriveMapDel ("X:")
EndIf
